package com.sunnada.demo.controller;

import com.sunnada.demo.bean.Dog;
import com.sunnada.demo.log.SpringUtil;
import com.sunnada.demo.log.annotation.LogRecord;
import com.sunnada.demo.service.DogService;
import com.sunnada.demo.service.serviceimpl.DogServiceImplTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

import static com.sunnada.demo.common.Util.resultToJson;

/**
 * @BelongsProject: mybatisproject2
 * @BelongsPackage: com.sunnada.demo.controller
 * @Author: 李瑞益
 * @CreateTime: 2018-10-08 09:42
 * @Description: MongoDBcontronller
 */
@Controller
@RequestMapping("/mongodb")
public class DogController {

    @Resource
    private DogServiceImplTemplate dogService;

    @RequestMapping("/getList")
    public String getList(Model model){
        List<Dog> list = dogService.getList();
        model.addAttribute("DataList",list);
        Object dogRepository = SpringUtil.getBean("catDao");
//        Object obj = TrackLogUtil_test.getData(dogRepository,6);
//        System.out.println("1322222222"+obj);
        return "/mongodb/mongo";
    }

@LogRecord("更新dog对象的信息")
@RequestMapping("/save")
@ResponseBody
    public String update(Dog dog){
        dogService.update(dog);
        return resultToJson(true);
    }
@LogRecord("删除dog对象的信息")
@RequestMapping("/delete")
    public String delete(@RequestParam("id") String id){
        dogService.delete(id);
        return "redirect:getList";
    }
@LogRecord("增加dog对象的信息")
    @RequestMapping("/add")
    @ResponseBody
    public String add(Dog dog){
        dogService.insert(dog);
        return resultToJson(true);
    }

    /**
     * 传入age和name，后台模糊
     * @param name
     * @param age
     * @param model
     * @return
     */
    @RequestMapping(value="/select",method = RequestMethod.GET)
    public String select(String name,String age,Model model){
        List<Dog> byNameAndAge = dogService.findByNameAndAge(name, age);
        model.addAttribute("DataList",byNameAndAge);
        return "/mongodb/mongo";
    }

    @RequestMapping(value="/selectName")
    public String select(String name,Model model){
        List<Dog> byName = dogService.findByName(name);
        model.addAttribute("DataList",byName);
        return "/mongodb/mongo";
    }

}
